﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using ARR.Entities;
using ARR.DataModel.Repositories;
using ARR.Services.Domain;

namespace ARR.Services
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "HistoryService" in both code and config file together.
    public class HistoryService : IHistoryService
    {
        public List<History> GetByCountry(int countryId, bool lazyLoad = true)
        {
            var histories = HistoryRepository.Select(x => x.CountryId == countryId).ConvertAll<History>(x => new History(x));

            if (!lazyLoad)
            {
                //TODO: Cargar aqui el HistoryDetails
            }

            histories.ForEach(x => x.ChangeState(DDF.Entity.EntityState.Original));

            return histories;
        }

        public List<History> GetAll(bool lazyLoad = true)
        {
            var histories = HistoryRepository.Select().ConvertAll<History>(x => new History(x));

            var countries = CountriesRepository.Select();

            foreach (var h in histories)
            {
                var c = countries.SingleOrDefault(x => x.Id == h.CountryId);

                if (c != null)
                    h.CountryName = c.Name;
            }


            if (!lazyLoad)
            {
                //TODO: Cargar aqui el HistoryDetails
            }

            histories.ForEach(x => x.ChangeState(DDF.Entity.EntityState.Original));

            return histories;
        }

        public void GenerateHistory(int rangeReviewId)
        {
            HistoryDomain.GenerateHistory(rangeReviewId);
        }

        public List<HistoryDetail> GetCurrentRangeReviewHistory(int rangeReviewId)
        {
            return HistoryDomain.GetCurrentRangeReviewHistory(rangeReviewId);
        }
    }
}
